import type { NextConfig } from 'next';

const nextConfig: NextConfig = {
  reactStrictMode: false, // 在开发环境中禁用严格模式以减少WebSocket重复连接
  serverExternalPackages: ['ws', 'web3', 'ethers', '@prisma/client'],
  webpack: (config, { isServer }) => {
    if (isServer) {
      // Handle node modules for server-side
      config.externals.push({
        'ws': 'commonjs ws',
        'web3': 'commonjs web3',
        'ethers': 'commonjs ethers',
        'pg': 'commonjs pg',
        'redis': 'commonjs redis',
      });
    }

    // SVG handling
    config.module.rules.push({
      test: /\.svg$/,
      use: ['@svgr/webpack'],
    });

    return config;
  },
  env: {
    // Database configuration
    DATABASE_URL: process.env.DATABASE_URL,
    
    // Redis configuration  
    REDIS_URL: process.env.REDIS_URL,
    
    // Blockchain RPC URLs
    ETHEREUM_RPC_URL: process.env.ETHEREUM_RPC_URL,
    BITCOIN_RPC_URL: process.env.BITCOIN_RPC_URL,
    BSC_RPC_URL: process.env.BSC_RPC_URL,
    POLYGON_RPC_URL: process.env.POLYGON_RPC_URL,
    SOLANA_RPC_URL: process.env.SOLANA_RPC_URL,
    
    // Monitoring configuration
    MIN_TRANSACTION_VALUE_USD: process.env.MIN_TRANSACTION_VALUE_USD,
    WHALE_THRESHOLD_USD: process.env.WHALE_THRESHOLD_USD,
    
    // Alert configuration
    EMAIL_SERVICE: process.env.EMAIL_SERVICE,
    EMAIL_USER: process.env.EMAIL_USER,
    EMAIL_PASSWORD: process.env.EMAIL_PASSWORD,
  },
  serverRuntimeConfig: {
    // Server-side only config
    DATABASE_URL: process.env.DATABASE_URL,
    REDIS_URL: process.env.REDIS_URL,
  },
  publicRuntimeConfig: {
    // Client-side accessible config
    APP_NAME: 'ChainWatch',
    API_BASE_URL: process.env.API_BASE_URL || '',
  },
  async rewrites() {
    return [
      {
        source: '/api/ws',
        destination: '/api/ws/route',
      },
    ];
  },
  async headers() {
    return [
      {
        source: '/api/:path*',
        headers: [
          {
            key: 'Access-Control-Allow-Origin',
            value: '*',
          },
          {
            key: 'Access-Control-Allow-Methods',
            value: 'GET, POST, PUT, DELETE, OPTIONS',
          },
          {
            key: 'Access-Control-Allow-Headers',
            value: 'Content-Type, Authorization',
          },
        ],
      },
    ];
  },
};

export default nextConfig;
